﻿
using Hotel.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hotel.DAL
{
    public class HouseTypeDAL
    {

        public HouseTypeDAL(MyConText myConText)
        {
            MyConText = myConText;
        }

        public MyConText MyConText { get; }



        /// <summary>
        /// 房型添加
        /// </summary>
        /// <param name="houseTypeModel"></param>
        /// <returns></returns>
        public int HouseTypeAdd(HouseTypeModel houseTypeModel)
        {

            MyConText.houseTypes.Add(houseTypeModel);
            return MyConText.SaveChanges();
        }




        /// <summary>
        /// 房型显示
        /// </summary>
        /// <param name="count"></param>
        /// <param name="page"></param>
        /// <param name="HouseTypeName"></param>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <returns></returns>

        public List<HouseTypeModel> HouseTypeList(out int count, out int page, string? HouseTypeName, int index = 1, int size = 2)
        {
            var list = MyConText.houseTypes.Where(x => x.IsDeleted == false).AsQueryable();

            if (!string.IsNullOrEmpty(HouseTypeName))
            {
                list = list.Where(x => x.HouseTypeName.Contains(HouseTypeName));

            }
            count = list.Count();
            page = (int)Math.Ceiling(count * 1.0 / size);
            return list.OrderBy(x => x.Id).Skip((index - 1) * size).Take(size).ToList();

        }




        /// <summary>
        /// 房型删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int HouseTypeDel(int id)
        {
            var HouseTypeDels = MyConText.houseTypes.Find(id);
            HouseTypeDels.IsDeleted = true;
            return MyConText.SaveChanges();
        }





        ///-----------------------------------------------------------------------------------------------------

        /// <summary>
        /// 房号添加
        /// </summary>
        /// <param name="houseNoModel"></param>
        /// <returns></returns>
        public int HouseNoAdd(HouseNoModel houseNoModel)
        {

            MyConText.houseNos.Add(houseNoModel);
            return MyConText.SaveChanges();

        }




        /// <summary>
        /// 房号显示
        /// </summary>
        /// <returns></returns>
        public List<HouseNoModel> HouseNoList()
        {
            return MyConText.houseNos.ToList();
        }




        /// <summary>
        ///房号反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public HouseNoModel HouseNoAll(int id)
        {
            return MyConText.houseNos.Find(id);
        }





        /// <summary>
        /// 房号修改
        /// </summary>
        /// <param name="HouseNoModel"></param>
        /// <returns></returns>
        public int HouseNoUpd(HouseNoModel HouseNoModel)
        {
            var HouseNoUpds = MyConText.houseNos.Find(HouseNoModel.Id);
            if (HouseNoUpds != null)
            {
                HouseNoUpds.HouseTypeId = HouseNoModel.HouseTypeId;
                HouseNoUpds.BeiZhu = HouseNoModel.BeiZhu;
                HouseNoUpds.State = HouseNoModel.State;
            }
            return MyConText.SaveChanges();
        }





        /// <summary>
        /// 批量修改
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="State"></param>
        /// <returns></returns>
        public int HouseNoState(string ids, int State)
        {

            var idsarr = ids.Split(",");
            foreach (var id in idsarr) 
            {
            
            var  infor=MyConText.houseNos.Find(Convert.ToInt32(id));
                infor.State = State;

            }
            return MyConText.SaveChanges();

        }
      
      






    }
}
